<?php
//dezend by http://www.yunlu99.com/ QQ:270656184
defined('BASEPATH') || exit('No direct script access allowed');
class Items extends MY_Controller
{
	private $item_status;

	public function __construct()
	{
		parent::__construct();
		$this->verify_user();
		$this->item_status = array('未提交', '待审核', '进行中', '已成功', '即将开始', '失败', '未通过', '已发放代币');
	}

	public function index()
	{
		$d['coins'] = $this->d->rows(array('table' => 'coin'));
		$d['status'] = $this->item_status;
		$this->view('items', $d);
	}

	public function data()
	{
		$page = ($this->input->post('page') ? intval($this->input->post('page')) : 1);
		$rows = ($this->input->post('rows') ? intval($this->input->post('rows')) : 10);
		$id = intval($this->input->post('id'));
		$status = $this->input->post('status');
		$username = $this->input->post('username');
		$start = $this->input->post('start');
		$end = $this->input->post('end');
		$coin_id = $this->input->post('coin_id');
		$where['status>='] = 1;

		if ($username) {
			$user = $this->db->where('username', $username)->get('user')->row_array();
			$where['user_id'] = $user['id'];
		}

		if ($start) {
			$where['add_time>='] = strtotime($start);
		}

		if ($end) {
			$where['add_time<='] = strtotime($end);
		}

		if ($id) {
			$where['id'] = $id;
		}

		if ($coin_id) {
			$where['coin_id'] = $coin_id;
		}

		if ((0 <= $status) && ($status != NULL)) {
			$where['status'] = $status;
		}

		$result['total'] = $this->db->where($where)->count_all_results('items');
		$start = ($page - 1) * $rows;
		$list = $this->db->where($where)->limit($rows, $start)->get('items')->result_array();

		foreach ($list as $k => $v) {
			$list[$k]['add_time'] = date('Y-m-d H:i', $v['add_time']);
			$user = $this->db->where('id', $v['user_id'])->get('user')->row_array();
			$list[$k]['username'] = $user['username'];

			if ($v['status'] == 2) {
				if (time() < $v['begin']) {
					$list[$k]['stat'] = 0;
				}
				else {
					if (($v['begin'] < time()) && (time() < $v['end'])) {
						if ($v['is_over'] == 1) {
							$list[$k]['stat'] = 1;
						}
						else if ($v['target_amount'] <= $v['deal']) {
							$list[$k]['stat'] = 2;
						}
						else {
							$list[$k]['stat'] = 1;
						}
					}
					else if ($v['end'] < time()) {
						if ($v['target_amount'] <= $v['deal']) {
							$list[$k]['stat'] = 2;
						}
						else {
							$list[$k]['stat'] = 3;
						}
					}
				}
			}

			$list[$k]['db_name'] = $this->d->field(array('table' => 'coin', 'field' => 'en_name', 'where' => 'id=' . $v['db_id']));
		}

		$result['rows'] = $list;
		echo json_encode($result);
	}

	public function add()
	{
		$d['coins'] = $this->db->get('coin')->result_array();
		$d['db_coins'] = $this->db->where('is_db', 1)->get('coin')->result_array();
		$this->load->view('items_add', $d);
	}

	public function insert()
	{
		$user_id = $this->input->post('user_id');
		$name = $this->input->post('name');
		$intro = $this->input->post('intro');
		$day = $this->input->post('day');
		$begin = $this->input->post('begin');
		$end = $this->input->post('end');
		$logo = $this->input->post('logo');
		$img = $this->input->post('img');
		$content = $this->input->post('content');
		$target_amount = $this->input->post('target_amount');
		$coin_id = $this->input->post('coin_id');
		$repay_day = $this->input->post('repay_day');
		$book = $this->input->post('book');
		$db_id = $this->input->post('db_id');
		$db_total = $this->input->post('db_total');
		$db_rate = $this->input->post('db_rate');
		$is_over = intval($this->input->post('is_over'));
		$is_lock = intval($this->input->post('is_lock'));
		$is_lock_support = intval($this->input->post('is_lock_support'));
		$data = array('name' => $name, 'intro' => $intro, 'day' => $day, 'begin' => strtotime($begin), 'end' => strtotime($end), 'logo' => $logo, 'img' => $img, 'content' => $content, 'target_amount' => $target_amount, 'coin_id' => $coin_id, 'repay_day' => $repay_day, 'book' => $book, 'add_time' => time(), 'user_id' => $user_id, 'db_id' => $db_id, 'db_total' => $db_total, 'db_rate' => $db_rate, 'step' => 4, 'is_over' => $is_over, 'is_lock' => $is_lock, 'is_lock_support' => $is_lock_support, 'status' => 1);
		$rs = $this->db->insert('items', $data);

		if ($rs) {
			echo json_encode(array('msg' => '添加成功', 'error' => 0));
		}
		else {
			echo json_encode(array('msg' => '添加失败', 'error' => 1));
		}
	}

	public function edit($id)
	{
		$id = intval($id);
		$d = $this->db->where('id', $id)->get('items')->row_array();
		$d['coins'] = $this->db->get('coin')->result_array();
		$d['db_coins'] = $this->db->where('is_db', 1)->get('coin')->result_array();
		$this->load->view('items_edit', $d);
	}

	public function update()
	{
		$id = $this->input->post('id');
		$user_id = $this->input->post('user_id');
		$name = $this->input->post('name');
		$intro = $this->input->post('intro');
		$day = $this->input->post('day');
		$begin = $this->input->post('begin');
		$end = $this->input->post('end');
		$logo = $this->input->post('logo');
		$img = $this->input->post('img');
		$content = $this->input->post('content');
		$target_amount = $this->input->post('target_amount');
		$coin_id = $this->input->post('coin_id');
		$repay_day = $this->input->post('repay_day');
		$book = $this->input->post('book');
		$db_id = $this->input->post('db_id');
		$db_total = $this->input->post('db_total');
		$db_rate = $this->input->post('db_rate');
		$is_over = intval($this->input->post('is_over'));
		$is_lock = intval($this->input->post('is_lock'));
		$is_lock_support = intval($this->input->post('is_lock_support'));
		$data = array('name' => $name, 'intro' => $intro, 'day' => $day, 'begin' => strtotime($begin), 'end' => strtotime($end), 'logo' => $logo, 'img' => $img, 'content' => $content, 'target_amount' => $target_amount, 'coin_id' => $coin_id, 'repay_day' => $repay_day, 'book' => $book, 'add_time' => time(), 'user_id' => $user_id, 'db_id' => $db_id, 'db_total' => $db_total, 'db_rate' => $db_rate, 'is_over' => $is_over, 'is_lock' => $is_lock, 'is_lock_support' => $is_lock_support);
		$rs = $this->db->where('id', $id)->update('items', $data);

		if ($rs) {
			echo json_encode(array('msg' => '修改成功', 'error' => 0));
		}
		else {
			echo json_encode(array('msg' => '修改失败', 'error' => 1));
		}
	}

	public function del()
	{
		$id = $this->input->get('id');

		if ($id) {
			$rs = $this->db->where_in('id', $id)->delete('items');

			if ($rs) {
				echo json_encode(array('msg' => '删除成功', 'error' => 0));
			}
			else {
				echo json_encode(array('msg' => '删除失败', 'error' => 1));
			}
		}
		else {
			echo json_encode(array('msg' => '删除失败！至少选择一行数据', 'error' => 0));
		}
	}

	public function set_status()
	{
		$id = intval($this->input->post('id'));
		$status = intval($this->input->post('status'));
		$this->db->where('id', $id);
		$this->db->set('status', $status);

		if ($this->db->update('items')) {
			ajax_return(0, '操作成功');
		}
		else {
			ajax_return(1, '操作失败');
		}
	}

	public function repay()
	{
		$id = intval($this->input->post('id'));
		$status = intval($this->input->post('status'));
		$this->db->query('set autocommit=0');
		$this->db->query('lock tables t_xnb write, t_items write, t_support write');
		$item = $this->db->where('id', $id)->where('status', 2)->get('items')->row_array();
		if (!$item || !$item['db_id'] || ($item['status'] == 7)) {
			$this->db->query('rollback');
			$this->db->query('unlock tables');
			ajax_return(1, '找不到可用项目');
		}

		$rs = array();
		$support = $this->db->where('item_id', $id)->get('support')->result_array();

		foreach ($support as $v) {
			if (num($v['repay']) <= 0) {
				continue;
			}

			$rs[] = $this->db->where('user_id', $v['user_id'])->where('coin_id', $item['db_id'])->set('num', 'num+' . num($v['repay']), false)->update('xnb');
		}

		$rs[] = $this->db->where('id', $id)->set('status', $status)->update('items');

		if (correct($rs)) {
			$this->db->query('commit');
			$this->db->query('unlock tables');
			ajax_return(0, '操作成功');
		}
		else {
			$this->db->query('rollback');
			$this->db->query('unlock tables');
			ajax_return(1, '操作失败');
		}
	}

	public function upload()
	{
		$config['upload_path'] = $_SERVER['DOCUMENT_ROOT'] . '/upload/item/';
		$config['allowed_types'] = 'gif|jpg|png|pdf';
		$config['max_size'] = 5000;
		$config['file_name'] = rand(1000, 9999) . time();
		$this->load->library('upload', $config);

		if (!$this->upload->do_upload('Filedata')) {
			$data['error'] = 1;
			$data['msg'] = '上传失败';
		}
		else {
			$data['error'] = 0;
			$data['msg'] = '上传成功';
			$d = $this->upload->data();
			$data['file_name'] = $d['file_name'];
		}

		echo json_encode($data);
	}
}

?>
